package com.food.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.food.dao.service.ManagerService;
import com.food.dao.service.MemberService;
import com.food.model.Manager;
import com.food.model.Member;

@WebServlet(
		urlPatterns={"/top/login.controller"}
)
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// �������
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				
				Map<String, String> errors = new HashMap<String, String>();
				request.setAttribute("errors", errors);
				
				if(username.equals("aegis105") || username.equals("z850062") || username.equals("ruby") || username.equals("eeit6713")){
					ManagerService managerService = new ManagerService();
					Manager manager = managerService.login(username, password);
					if(manager==null) {
						errors.put("password", "Login failed, please try again.");
						request.getRequestDispatcher(
								"/login/login.jsp").forward(request, response);
					} else {
						HttpSession session = request.getSession();
						session.setAttribute("manager", manager);
						
						String dest = (String) session.getAttribute("dest");
						if(dest!=null && dest.length()!=0) {
							session.removeAttribute("dest");
							response.sendRedirect(dest);
						} else {
							String path = request.getContextPath();
							response.sendRedirect(path+"/index.jsp");
							return;
						}
					}
					
				}
				
				
		// ���Ҹ��
				

				if(username==null || username.trim().length()==0) {
					errors.put("username", "Please enter ID");
				}
				if(password==null || password.trim().length()==0) {
					errors.put("password", "Please enter PWD");
				}
				
				if(errors!=null && !errors.isEmpty()) {
					request.getRequestDispatcher(
							"/login/login.jsp").forward(request, response);
					return;
				}

		// �I�sModel
				MemberService service = new MemberService();
				Member bean = service.login(username, password);
				
		//�ھ�Model���浲�G�I�sView
				if(bean==null) {
					errors.put("password", "Login failed, please try again.");
					request.getRequestDispatcher(
							"/login/login.jsp").forward(request, response);
				} else {
					HttpSession session = request.getSession();
					session.setAttribute("user", bean);
					
					String dest = (String) session.getAttribute("dest");
					if(dest!=null && dest.length()!=0) {
						if(request.getParameter("cid")!=null){
							System.out.println("ciddddd");
							String cid = request.getParameter("cid");
							session.removeAttribute("dest");
							response.sendRedirect(dest+"?cid="+cid);
							return;
						}
						session.removeAttribute("dest");
						response.sendRedirect(dest);
						return;
					} else {
						String path = request.getContextPath();
						response.sendRedirect(path+"/index.jsp");
						return;
					}
				}
			}

}
